package com.honey.solr.daosupport.method;

import com.honey.compilation.java.IfBlock;
import com.honey.compilation.java.JavaVisibility;
import com.honey.compilation.java.Method;
import com.honey.compilation.java.MethodDecoration;
import com.honey.compilation.java.Parameter;
import com.honey.core.builder.CharacterBuilder;
import com.honey.core.builder.CharacterBuilderFactory;
import com.honey.core.compiler.IndentSpace;
import com.honey.core.types.FullyQualifiedJavaType;
import com.honey.core.types.JDKFullyQualifiedJavaType;
import com.honey.generator.java.AbstractClassName;

public class BetweenMethod extends AbstractClassName {
	
	public Method createMethod(){
		Method answer = new Method("between");
		MethodDecoration methodDecoration = answer.getDecoration();
		methodDecoration.setVisibility(JavaVisibility.PRIVATE);
		methodDecoration.setFinal(true);
		answer.setReturnType(new FullyQualifiedJavaType("StringBuilder"));
		Parameter parameter = new Parameter("Enum<?>","name");parameter.addJavaDocLine("查询字段的名称");
		answer.addParameter(parameter);
		parameter = new Parameter(JDKFullyQualifiedJavaType.getStringInstance(),"value1");
		answer.addParameter(parameter);parameter.addJavaDocLine("第一个值");
		parameter = new Parameter(JDKFullyQualifiedJavaType.getStringInstance(),"value2");
		answer.addParameter(parameter);parameter.addJavaDocLine("第二个值");
		answer.addBodyLine("StringBuilder answer = new StringBuilder()");
		IfBlock ifblock = new IfBlock("value1 == null || value1.length()==0");
		ifblock.addBodyLine("value1 =\"*\"");
		answer.addBlocks(ifblock);
		ifblock = new IfBlock("value2 == null || value2.length()==0");
		ifblock.addBodyLine("value2 =\"*\"");
		answer.addBlocks(ifblock);
		//
		CharacterBuilder sb = CharacterBuilderFactory.createDefaultStringBuilder();
		sb.append("answer.append(name).append(new char[]{':','['})");IndentSpace.newSpace(sb, 3);
		sb.append(".append(value1).append(new char[]{' ','T','O',' '}).append(value2)");IndentSpace.newSpace(sb, 3);
		sb.append(".append(']')");IndentSpace.newSpace(sb, 3);
		
		answer.addBodyLine(sb.toString());
		answer.addBodyLine("return answer");
		
		answer.addJavaDocLine("等于区间查询. Lucene的区间查询格式是: field:[ value1 TO value2 ], 意义如下:<br />");
		answer.addJavaDocLine("<ul>");
		answer.addJavaDocLine("<li>value1和value2同时不为null, 意义是: field>=value1 and field<=value2 </li>");
		answer.addJavaDocLine("<li>value1为null value2不为null, 意义是: field>=value1</li>");
		answer.addJavaDocLine("<li>value1不为null value2为null, 意义是: field<=value2</li>");
		answer.addJavaDocLine("</ul>");
		return answer ;
	}
}
